package paixu;

import java.util.Scanner;

public class Find {
    public static void main(String[] args) {
        int [] arr=new int[]{1,2,3,4,5,6,7,8,10,80,90,100};
        //直接查找：
        Scanner s = new Scanner(System.in);
        System.out.print("请输入您想要查找的数字:");
        int n = s.nextInt();
        findNum(arr,n);
        System.out.println();
        findNum1(arr,n);
    }
    //直接
    public static void findNum(int arr [],int n){
        int k = 0;
        boolean isFlag = false;
        for (int i = 0 ; i <arr.length;i++){
            if(n==arr[i]){
                k=i;
                isFlag=true;
                break;
            }
        }
        if(isFlag){
            System.out.println("找到了在数组的第"+(k+1)+"个，数值为："+arr[k]);
        }else {
            System.out.println("没有该数值");
        }
    }
    //二分法:
    public static void findNum1(int [] arr,int n ){
        int begin=0;
        int end=arr.length;
        int mind =0;
        boolean isFlag=false;
        while (begin<=end){
            mind=(begin+end)/2;
            if(arr[mind]==n){
                isFlag=true;
                break;
            }
            if(arr[mind]>n){
                end=mind-1;
            }
            if(arr[mind]<n){
                begin=mind+1;
            }
        }
        if (isFlag){
            System.out.println("找到了,为数组的第"+(mind+1)+"个，值为："+n);
            return;
        }else {
            System.out.println("没有该数字");
            return;
        }
    }

}
